package io.github.talelin.latticy.controller.v1;

import com.alibaba.excel.EasyExcel;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import io.github.talelin.latticy.dto.oms.product.ProductDTO;
import io.github.talelin.latticy.dto.oms.product.ProductPage;
import io.github.talelin.latticy.execl.oms.Product.ProductDataListener;
import io.github.talelin.latticy.execl.oms.Product.ProductExecl;
import io.github.talelin.latticy.feign.WmsCategoryFeignClient;
import io.github.talelin.latticy.model.oms.ProductDO;
import io.github.talelin.latticy.service.oms.ProductService;
import io.github.talelin.latticy.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Positive;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName：ProductController
 * @Author: ljj
 * @Date: 2022/5/10 9:17
 */
@RestController
@RequestMapping("/v1/product")
@PermissionModule(value = "产品管理")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private WmsCategoryFeignClient wmsCategoryFeignClient;

    @PostMapping("/add")
    @PermissionMeta(value = "创建产品")
    @GroupRequired
    public CreatedVO create(@RequestBody @Validated ProductDTO productDTO) {
        productService.add(productDTO);
        return new CreatedVO();
    }

    @PermissionMeta(value = "分页查询产品")
    @GroupRequired
    @PostMapping("/pageList")
    public FindResultVO pageList(@RequestBody ProductPage productPage) {
        return FindResultVO.success(productService.pageList(productPage));
    }

    @PermissionMeta(value = "修改产品状态")
    @PostMapping("/updateStatus")
    @GroupRequired
    public UpdatedVO updateStatus(@RequestBody ProductDTO productDTO) {
        productService.updateStatus(productDTO);
        return new UpdatedVO();
    }

    @PermissionMeta(value = "修改产品")
    @GroupRequired
    @PostMapping("/update")
    public UpdatedVO update(@RequestBody @Validated ProductDTO productDTO) {
        productService.update(productDTO);
        return new UpdatedVO();
    }

    @PermissionMeta(value = "删除产品")
    @GroupRequired
    @PostMapping("/softDelete")
    public DeletedVO softDelete(@RequestBody List<Long> ids) {
        productService.softDelete(ids);
        return new DeletedVO();
    }

    @PermissionMeta(value = "删除产品")
    @GroupRequired
    @PostMapping("/delete")
    public DeletedVO delete(@RequestBody List<Long> ids) {
        productService.delete(ids);
        return new DeletedVO();
    }

    @PermissionMeta(value = "批量设置包材")
    @GroupRequired
    @PostMapping("/batchSetPackagMaterial")
    public UpdatedVO batchSetPackagMaterial(@RequestBody ProductDTO productDTO) {
        productService.batchSetPackagMaterial(productDTO);
        return new UpdatedVO();
    }

    @PermissionMeta(value = "批量设置预警库存")
    @GroupRequired
    @PostMapping("/batchSetAlarmThreshold")
    public UpdatedVO batchSetAlarmThreshold(@RequestBody ProductDTO productDTO) {
        productService.batchSetAlarmThreshold(productDTO);
        return new UpdatedVO();
    }

    @PermissionMeta(value = "批量导入")
    @GroupRequired
    @PostMapping("/batchImport")
    public ImportVO batchImport(@RequestParam("file") MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), ProductExecl.class, new ProductDataListener(productService)).sheet().doRead();
        return new ImportVO();
    }

    @PermissionMeta(value = "导出")
    @GroupRequired
    @GetMapping("/export/{type}")
    public void export(@PathVariable Integer type, HttpServletResponse response) throws IOException {
        productService.export(type,response);
    }

    @PermissionMeta(value = "导入图片压缩包")
    @GroupRequired
    @PostMapping("/uploadImageZip")
    public ImportVO uploadImageZip(@RequestParam("file") MultipartFile zipFile) {
        try {
            productService.uploadImageZip(zipFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ImportVO();
    }

    @PermissionMeta(value = "查看已审核的产品")
    @GroupRequired
    @GetMapping("/findAllByAudit")
    public FindResultVO findAllByAudit() {
        return FindResultVO.success(productService.findAllByAudit());
    }

    @PermissionMeta(value = "根据id查询")
    @GroupRequired
    @GetMapping("/findById/{id}")
    public FindResultVO findById(@PathVariable @Positive(message = "{id.positive}") Long id) {
        return FindResultVO.success(productService.findById(id));
    }

    @PermissionMeta(value = "根据产品sku查询")
    @GroupRequired
    @PostMapping("/getListBySkus")
    public List<ProductDO> getListBySkus(@RequestBody List<String> skus) {
        return productService.getListBySkus(skus);
    }

    /**
     * 打印pdf文件
     */
    /*@PostMapping("/barCode")
    @PermissionMeta(value = "打印条形码")
    @GroupRequired
    @Logger(template = "{user.name}打印条形码")
    public void getPdf(HttpServletResponse response, @RequestBody ProductDTO productDTO) {
        productService.getBarCode(response, productDTO);
    }*/

    /**
     * 打印pdf文件
     */
   /* @GetMapping("/barCode1")
    public void getPdf1(HttpServletResponse response) {
        PrintBarcodeDTO.productNum productNum = new PrintBarcodeDTO.productNum();
        productNum.setId(83L);
        productNum.setNum(1);
        List<PrintBarcodeDTO.productNum> productNums = new ArrayList<>();
        productNums.add(productNum);
        PrintBarcodeDTO printBarcodeDTO = new PrintBarcodeDTO();
        printBarcodeDTO.setEnName(true);
        printBarcodeDTO.setOrigin(true);
        printBarcodeDTO.setProductName(true);
        printBarcodeDTO.setType(1);
        printBarcodeDTO.setPageWidth(148);
        printBarcodeDTO.setPageHeight(90);
        printBarcodeDTO.setProductNums(productNums);
        printBarcodeDTO.setDate(false);
        productService.getBarCode(response, printBarcodeDTO);
    }*/

    @GetMapping("/getCategory")
    @PermissionMeta(value = "获取树形品类")
    @GroupRequired
    public FindResultVO getCategory() {
        return wmsCategoryFeignClient.getCategory();
    }

    @PermissionMeta(value = "根据id查询品类")
    @GetMapping("/getCategoryById/{id}")
    public FindResultVO getCategoryById(@PathVariable Long id) {
        return wmsCategoryFeignClient.getCategoryById(id.intValue());
    }

    @PermissionMeta(value = "查询预警产品")
    @GroupRequired
    @GetMapping("/findAlarmThreshold")
    public FindResultVO findAlarmThreshold(){
        return productService.findAlarmThreshold();
    }
}
